﻿using Net.Core;
using Net.IRepository;
using Net.IService;
using Net.Model.Entities.Systems;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Net.Service
{
    public class SysUserServices : BaseServices<SysUser>, ISysUserServices
    {
        private readonly IUnitOfWork _unitOfWork;
        private readonly ISysUserRepository _sysUserRepository;
        public SysUserServices(IUnitOfWork unitOfWork, ISysUserRepository sysUserRepository)
        {
            this._unitOfWork = unitOfWork;
            this._sysUserRepository = sysUserRepository;
            this.BaseDal = sysUserRepository;
        }

        //[Caching(AbsoluteExpiration = 30)]
        public async Task<string> GetUserRoleNameStr(string loginName, string loginPwd)
        {
            //var roleName = "";
            //var user = await QueryByClauseAsync(a => a.userName == loginName && a.passWord == loginPwd);
            //var roleList = await _sysRoleRepository.QueryListByClauseAsync(a => a.deleted == false);
            //if (user != null)
            //{
            //    var userRoles = await _sysUserRoleRepository.QueryListByClauseAsync(ur => ur.userId == user.id);
            //    if (userRoles.Count > 0)
            //    {
            //        var arr = userRoles.Select(ur => ur.roleId).ToList();
            //        var roles = roleList.Where(d => arr.Contains(d.id));

            //        roleName = string.Join(',', roles.Select(r => r.roleCode).ToArray());
            //    }
            //}

            //return roleName;

            var sysUser = await _sysUserRepository.QueryByClauseAsync(a => a.userName == loginName && a.passWord == loginPwd);
            if (sysUser != null)
            {
                return await Task.FromResult("用户登录成功");
            }
            return await Task.FromResult("用户登录失败");
        }
    }
}
